/**
 *  模拟没有头结点的单向链表 直接将首元节点作为头结点
 *
 */

public class SingleLinkedList {


    private class Node {
        int val;
        Node next;

        public Node() {
        }

        public Node(int val) {
            this.val = val;
        }

        public Node(int val, Node next) {
            this.val = val;
            this.next = next;
        }
    }

    public Node headNode = null;

    public SingleLinkedList() {
    }

    //头插法
    public void addFirst(int data) {
        if(headNode == null){
            Node node = new Node(data);
            headNode = node;
            return;
        }

        Node newNode = new Node(data);
        newNode.next = headNode;
        headNode= newNode;

    }

  //尾插法
    public void addLast(int data){
        Node node = headNode;
        while(node.next != null){
            node = node.next;
        }
        Node newNode = new Node(data);
        node.next = newNode;
    }

    //任意位置插入,第一个数据节点为0号下标
    //要在0号数据节点插入 用户输入的是index = 1
    public boolean addIndex(int index, int data){
        index = index -1;

        if(index < 0){
            return false;
        }
        if(headNode == null){
            headNode = new Node(data);
            return true;
        }

        //第一个节点直接头插
        if(index == 0){
            addFirst(data);
            return true;
        }

        //插入找前驱
        Node curPre = headNode;
        Node cur = headNode;
        for (int i = 0; i < index; i++) {
            if(cur == null){
                return false;
            }
            curPre = cur;
            cur = cur.next;
        }
        Node newNode = new Node(data);
        newNode.next = cur;
        curPre.next = newNode;
        return true;
    }



    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        if(headNode == null){
            return false;
        }
        Node cur = headNode;
        while(cur != null){
           return ((cur.val == key) ? true:false);
        }
        return false;
    }

    //删除第一次出现关键字为key的节点
    public void remove(int key){
        if(headNode == null){
            return;
        }
        //headNode为要删除的节点 直接删除
        if(headNode.val == key){
            headNode = headNode.next;
            return;
        }

        Node cur = headNode;
        Node curPre = headNode;
        //删除找前驱
        while(cur != null){
            curPre = cur;
            cur = cur.next;
            if(cur.val == key){
                curPre.next = cur.next;
                return;
            }
        }
    }

    //删除所有值为key的节点
    public void removeAllKey(int key){
        if(headNode == null){
            return;
        }
        //从第二个节点开始判断是否满足删除条件
        Node cur = headNode.next;
        Node curPre = headNode;
        while(cur != null){
            if(cur.val == key){
                curPre.next = cur.next;
            }else{
                curPre = cur;
            }
            cur = cur.next;
        }

        //再判断第一个节点是否满足删除条件
        if(headNode.val == key){
            headNode = headNode.next;
        }
    }


    //得到单链表的长度

    public int size(){
        int count = 0;
        Node cur = headNode;
        while(cur != null){
            count++;
        }
        return count;
    }

    public void display(){
        Node cur = headNode;
        while(cur != null){
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }

    public void clear(){
        headNode = null;
    }
}